package com.eva.framework.common.model;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Data
@Schema(description = "分页对象")
public class PageData<T> implements Serializable {

    @Schema(description = "目标页")
    private long page;

    @Schema(description = "页容量")
    private long capacity;

    @Schema(description = "总记录数")
    private long total;

    @Schema(description = "当前页的数据")
    private List<T> records;

    public PageData(long page, long capacity) {
        this.page = page;
        this.capacity = capacity;
    }

    /**
     * 获取空页对象
     *
     * @return 分页对象
     */
    public static <T> PageData<T> empty () {
        PageData<T> pageData = new PageData<>(1, 10);
        pageData.total = 0;
        pageData.records = Collections.emptyList();
        return pageData;
    }

    /**
     * 处理异常页容量
     */
    public long getCapacity () {
        return capacity <= 0 ? 10 : capacity;
    }

    /**
     * 获取总页数
     */
    @Schema(description = "总页数")
    public long getPageCount(){
        if(this.getTotal() % this.getCapacity() == 0){
            long pc = this.getTotal()/this.getCapacity();
            return pc == 0 ? 1 : pc;
        }
        return this.getTotal()/this.getCapacity() + 1;
    }

}
